$(function() {
	var end = false;
	var pageCount=0;
	var pageNum=20;
	render();
	$("#config-btn").click(function() {
		$("#config-modal").modal({
			show : true,
			keyboard : true
		});
		$.get(path + "/alarm/alarmConfig", function(data) {
			var json = JSON.parse(data);
			if (json.faliureTrigger == true) {
				$("#faliureTrigger").prop({"checked": true});
			}
			$("#agentThreshold").val(json.agentThreshold);
			$("#serviceThreshold").val(json.serviceThreshold);
			$("#waitingTimeThreshold").val(json.waitingTimeThreshold);
			$("#unProcessNumber").val(json.unProcessThreshold);
			$("#failureRateTheshold").val(json.failureRateTheshold);
			$("#sendTo").val(json.sendTo);
			$("#healthUrl").val(json.healthUrl);
		});
	});
	
	$("#prev").click(function() {
		if(pageCount-1>=0){
			pageCount=pageCount-1;
			var fromNum=pageCount*pageNum;
			$.get(path + "/alarm/alarmInfo?fromNum="+fromNum+"&pageNum="+pageNum, function(data) {
				var status = data.status;
				for(var i = 0; i < status.length; i++){
					if(status[i].isNotified === 'N'){
						status[i].alarmCls = 'error';
					}else{
						status[i].alarmCls = '';
					}
				}
				renderAgentStatus(data);
			});
		}
	});
	$("#next").click(function() {
		if(end)
			return;
		
		pageCount=pageCount+1;
		var fromNum=pageCount*pageNum;
		$.get(path + "/alarm/alarmInfo?fromNum="+fromNum+"&pageNum="+pageNum, function(data) {
			var status = data.status;
			if(status.length == 0) {
				end = true;
				return;
			}
			for(var i = 0; i < status.length; i++){
				if(status[i].isNotified === 'N'){
					status[i].alarmCls = 'error';
				}else{
					status[i].alarmCls = '';
				}
			}
			renderAgentStatus(data);
		});
	
	});
	function render() {
		$.get(path + "/alarm/alarmInfo?fromNum=0&pageNum="+pageNum, function(data) {
			var status = data.status;
			for(var i = 0; i < status.length; i++){
				if(status[i].isNotified === 'N'){
					status[i].alarmCls = 'error';
				}else{
					status[i].alarmCls = '';
				}
			}
			renderAgentStatus(data);

		});
	}
	$(".update-btn").click(function() {
		render();
	});
	function renderAgentStatus(data) {
		$("#alarm").empty().prepend($.renderTmpl("alarmInfo", data));
	}
	
	$("#alarm_inspector").click(function(){
		$(this).button('loading');
		var trigger = 0;
		if ($("#faliureTrigger").prop("checked") == true) {
			trigger = 1;
		}
		var self = this;
		var data = {
				"faliureTrigger" : trigger,
				"agentThreshold" : $("#agentThreshold").val(),
				"serviceThreshold" : $("#serviceThreshold").val(),
				"waitingTimeThreshold" : $("#waitingTimeThreshold").val(),
				"unProcessThreshold" : $("#unProcessNumber").val(),
				"failureRateTheshold" : $("#failureRateTheshold").val(),
				"sendTo" : $("#sendTo").val(),
				"healthUrl" : $("#healthUrl").val()
		};
		$.ajax({
			url : "alarm/healthCheck",
			type : "post",
			contentType : 'application/json',
			data : JSON.stringify(data),
			success : function(data){
				if(data.result == "success")
				{
					$(self).button('complete');
				}
				else 
				{
					$(self).button('error');
				}	
			},
			error : function(){
				$(self).button('error');
			}
		});
		return false;
	})
	
	$("#alarm_form").submit(function() {
		var trigger = 0;
		if ($("#faliureTrigger").prop("checked") == true) {
			trigger = 1;
		}
		var data = {
			"faliureTrigger" : trigger,
			"agentThreshold" : $("#agentThreshold").val(),
			"serviceThreshold" : $("#serviceThreshold").val(),
			"waitingTimeThreshold" : $("#waitingTimeThreshold").val(),
			"unProcessThreshold" : $("#unProcessNumber").val(),
			"failureRateTheshold" : $("#failureRateTheshold").val(),
			"sendTo" : $("#sendTo").val(),
			"healthUrl" : $("#healthUrl").val()
		};
		$.ajax({
			url : "alarm/alarmConfig",
			type : "post",
			contentType : 'application/json',
			data : JSON.stringify(data),
			success : function() {
				if (parent) {
					parent.$("#config-modal").modal('hide');
				}
			}
		});

		return false;
	});
});
